<template>
  <view>
    <anilSeat
        :title="movieInfo.name"
        :info="timeData"
        :room-name="officeInfo.roomname"
        :max="max"
        :seatData="dataList"
        @confirm="getInfo"
    ></anilSeat>
  </view>
</template>

<script setup>
import {ref} from 'vue';
import {onLoad} from '@dcloudio/uni-app';
import anilSeat from "./components/anil-seat.vue";
import sheep from "@/sheep";

// 响应式数据
const dataList = ref([]);
const max = ref(4);
const cinemaId = ref("");
const movieInfo = ref({});
const officeInfo = ref({});
const timeData = ref("");
const seats = ref([]);

// 生命周期钩子 - 页面加载
onLoad((opt) => {
  console.log('seat->opt: ', opt)
  movieInfo.value = JSON.parse(opt.chooseMovie);
  officeInfo.value = JSON.parse(opt.movieItem);

  console.log('电影信息: ', movieInfo.value);
  console.log('选座opi信息: ', officeInfo.value);
  cinemaId.value = officeInfo.value.cinemaId || '';
  timeData.value = formatTime(officeInfo.value.playtime);
  getData();
});

// 方法 - 获取座位数据
const getData = async () => {
  const res = await sheep.$api.xkMovie.getOpiSeat({
    opiId: officeInfo.value.opiid
  });

  if (res.code === 200 && res.data?.seatList) {
    seats.value = res.data.seatList;
    formatSeatData(res.data.seatList);
  } else {
    throw new Error(res?.message || '获取座位数据失败');
  }
};

// 方法 - 格式化座位数据
const formatSeatData = (seatList) => {

  const formattedData = seatList.map(item => ({
    columnNo: item.column,
    rowNo: item.row,
    seatId: item.seatid,
    seatName: item.name,
    pai: item.row,
    lie: item.column,
    status: item.status,
    price: officeInfo.value.showPrice / 100,
  }));

  dataList.value = formattedData.map(item => ({
    YCoord: item.rowNo,
    XCoord: item.columnNo,
    SeatCode: item.seatName,
    SeatId: item.seatid,
    Status: item.status,
    RowNum: item.pai,
    Price: item.price,
    ColumnNum: item.lie
  }))
};

// 方法 - 处理选座确认
const getInfo = (selectedSeats) => {
  console.log('选座坐标信息: ', selectedSeats);
  if (!selectedSeats || selectedSeats.length === 0) {
    uni.showToast({title: '请选择座位', icon: 'none'});
    return;
  }

  let totalPrice = 0;
  const seatParams = selectedSeats.map(item => {

    const seat = seats.value.find(seat => seat.name === item.SeatCode);
    if (!seat) return null;
    console.log('seatInfo: ', item);
    totalPrice += item.Price;
    return {
      row: item.RowNum,
      col: item.ColumnNum,
      seatName: item.SeatCode,
      seatId: seat.seatid,
      price: item.Price
    };
  }).filter(Boolean);
  let prm = {
    officeInfo: officeInfo.value,
    cinemaId: cinemaId.value,
    movieInfo: movieInfo.value,
    showInfo: seatParams,
    price: totalPrice,
  };
  sheep.$router.go('/pages/xk-movie/confirm-order', {prm: JSON.stringify(prm)});
};

// 方法 - 格式化时间
// 工具方法 - 时间格式化
const formatTime = (timestamp) => {
  try {
    let split = timestamp.split('.');
    const date = new Date(split[0]);
    const hours = date.getHours().toString().padStart(2, '0');
    const minutes = date.getMinutes().toString().padStart(2, '0');
    return `${hours}:${minutes}`;
  } catch (err) {
    console.error('时间格式化失败:', err);
    return timestamp;
  }
};

</script>

<style lang="scss" scoped></style>
